import { h, ref } from 'vue';
import { NTag } from 'naive-ui';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { isNullObject } from '@/utils/is';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { validate } from '@/utils/validateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // ID
  public ordernum = ''; // 订单号
  public recycler = null; // 服务天使
  public category = null; // 分类
  public sort = null; // 排序
  public uid = null; // 用户ID
  public name = ''; // 姓名
  public phone = ''; // 电话
  public price = ''; // 金额
  public weight = ''; // 预估重量
  public totalMoney = ''; // 总金额
  public appointmenttime = ''; // 预约时间
  public status = 1; // 状态
  public address = ''; // 地址
  public delreason = null; // 取消原因
  public delcontent = ''; // 取消备注
  public createdAt = ''; // created_at
  public updatedAt = ''; // updated_at
  public deletedAt = ''; // deleted_at

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  ordernum: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入订单号',
  },
  recycler: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入服务天使',
  },
  category: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入分类',
  },
  sort: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入排序',
  },
  uid: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入用户ID',
  },
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入姓名',
  },
  phone: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入电话',
  },
  price: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.amount,
  },
  weight: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入预估重量',
  },
  totalMoney: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入总金额',
  },
  appointmenttime: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入预约时间',
  },
  status: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入状态',
  },
  address: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入地址',
  },
  delreason: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入取消原因',
  },
  delcontent: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入取消备注',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: 'ID',
    componentProps: {
      placeholder: '请输入ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态',
      options: dict.getOption('orderStatus'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: 'created_at',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '订单号',
    key: 'ordernum',
    align: 'left',
    width: -1,
  },
  {
    title: '服务天使',
    key: 'recycler',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('epRecyclerOption', row.recycler);
    },
  },
  {
    title: '分类',
    key: 'category',
    align: 'left',
    width: -1,
    render(row: State) {
      if (isNullObject(row.category) || !isArray(row.category)) {
        return ``;
      }
      return row.category.map((tagKey) => {
        return renderOptionTag('epCategoryOption', row.tagKey)
      });
    },
  },
  {
    title: '排序',
    key: 'sort',
    align: 'left',
    width: -1,
    render(row: State) {
      if (isNullObject(row.sort) || !isArray(row.sort)) {
        return ``;
      }
      return row.sort.map((tagKey) => {
        return renderOptionTag('testCategoryOption', row.tagKey)
      });
    },
  },
  {
    title: '用户ID',
    key: 'uid',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('epRecyclerOption', row.uid);
    },
  },
  {
    title: '姓名',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '电话',
    key: 'phone',
    align: 'left',
    width: -1,
  },
  {
    title: '金额',
    key: 'price',
    align: 'left',
    width: -1,
  },
  {
    title: '预估重量',
    key: 'weight',
    align: 'left',
    width: -1,
  },
  {
    title: '总金额',
    key: 'totalMoney',
    align: 'left',
    width: -1,
  },
  {
    title: '预约时间',
    key: 'appointmenttime',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('orderStatus', row.status);
    },
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['epRecyclerOption', 'epCategoryOption', 'testCategoryOption', 'orderStatus', 'acceptRefundStatus']);
}